// ignore_for_file: avoid_print

import 'package:flujs/flujs.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flujs_jsc/jsc.dart' as jsc;
import 'package:flujs_qjs/qjs.dart' as qjs;

void main() {
  runCookie(JSFRuntime rt, {bool withWrapper = false}) async {
    var ctx = rt.newContext()..loadExtension();
    var uri = 'http://localhost:3000/api/admin/signin';
    var ck = await ctx.evalAsync('''fetch('$uri', { method: 'GET'})''');
    print('ck1: $ck');
    // expect(ck.hasProperty('json'), true);
    // var ckJson = ck.getProperty('json').callAsFunction(thiz: ck);
    // if (ckJson.isPromise()) {
    //   var ret = await ctx.runPromise(ckJson);
    //   print('ck2: $ret');
    // }

    var getCookieFn =
        withWrapper ? 'flujs.cookie.get' : '__FLUJS_XHR_GET_COOKIE__';
    var ckre = await ctx.evalAsync('''$getCookieFn('$uri')''');
    print('1: cookie: ${ckre.jsonString()}');

    var deleteCookieFn =
        withWrapper ? 'flujs.cookie.delete' : '__FLUJS_XHR_DELETE_COOKIE__';
    await ctx.evalAsync('''$deleteCookieFn('$uri')''');

    ckre = await ctx.evalAsync('''$getCookieFn('$uri')''');
    print('2: cookieis null: ${ckre.jsonString()}');
  }

  test('ck_jsc', () async {
    var rt = jsc.getJSFRuntime();
    await runCookie(rt);
  });
  test('ck_qjs', () async {
    var rt = qjs.getJSFRuntime();
    await runCookie(rt);
  });

  test('ck_wrapper_jsc', () async {
    var rt = jsc.getJSFRuntime();
    await runCookie(rt, withWrapper: true);
  });
  test('ck_wrapper_qjs', () async {
    var rt = qjs.getJSFRuntime();
    await runCookie(rt, withWrapper: true);
  });
}
